package com.dxt.common;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.spec.KeySpec;

public class DES3Util {

	static String DES = "DES/ECB/NoPadding";
	static String TriDes = "DESede/ECB/NoPadding";



	public static String byte2hex(byte[] data) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < data.length; i++) {
			String temp = Integer.toHexString(((int) data[i]) & 0xFF);
			for (int t = temp.length(); t < 2; t++) {
				sb.append("0");
			}
			sb.append(temp);
		}
		return sb.toString();
	}

	public static byte[] hexToBytes(String str) {
		if (str == null) {
			return null;
		} else if (str.length() < 2) {
			return null;
		} else {
			int len = str.length() / 2;
			byte[] buffer = new byte[len];
			for (int i = 0; i < len; i++) {
				buffer[i] = (byte) Integer.parseInt(str.substring(i * 2,
						i * 2 + 2), 16);
			}
			return buffer;
		}
	}

	public static byte[] trides_crypt(byte key[], byte data[]) {
		try {
			byte[] k = new byte[24];

			int len = data.length;
			if (data.length % 8 != 0) {
				len = data.length - data.length % 8 + 8;
			}
			byte[] needData = null;
			if (len != 0)
				needData = new byte[len];

			for (int i = 0; i < len; i++) {
				needData[i] = 0x00;
			}

			System.arraycopy(data, 0, needData, 0, data.length);

			if (key.length == 16) {
				System.arraycopy(key, 0, k, 0, key.length);
				System.arraycopy(key, 0, k, 16, 8);
			} else {
				System.arraycopy(key, 0, k, 0, 24);
			}

			KeySpec ks = new DESedeKeySpec(k);
			SecretKeyFactory kf = SecretKeyFactory.getInstance("DESede");
			SecretKey ky = kf.generateSecret(ks);

			Cipher c = Cipher.getInstance(TriDes);
			c.init(Cipher.ENCRYPT_MODE, ky);
			return c.doFinal(needData);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	public static byte[] trides_decrypt(byte key[], byte data[]) {
		try {
			byte[] k = new byte[24];

			int len = data.length;
			if (data.length % 8 != 0) {
				len = data.length - data.length % 8 + 8;
			}
			byte[] needData = null;
			if (len != 0)
				needData = new byte[len];

			for (int i = 0; i < len; i++) {
				needData[i] = 0x00;
			}

			System.arraycopy(data, 0, needData, 0, data.length);

			if (key.length == 16) {
				System.arraycopy(key, 0, k, 0, key.length);
				System.arraycopy(key, 0, k, 16, 8);
			} else {
				System.arraycopy(key, 0, k, 0, 24);
			}
			KeySpec ks = new DESedeKeySpec(k);
			SecretKeyFactory kf = SecretKeyFactory.getInstance("DESede");
			SecretKey ky = kf.generateSecret(ks);

			Cipher c = Cipher.getInstance(TriDes);
			c.init(Cipher.DECRYPT_MODE, ky);
			return c.doFinal(needData);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static void main(String[] args) throws Exception {

		String fileUrl = "C:\\Users\\Gdo\\Desktop\\数据文件\\物联网\\文件\\USimMS_2401BIP2B95020130419152901689362_2_240_1_20130419153300.dat";
		String fileUrl2 = "C:\\Users\\Gdo\\Desktop\\数据文件\\物联网\\文件\\decode.dat";
//		desDecryptFile(fileUrl, fileUrl2, "1F26729F19CAF901A855C0420882CC85");
		String ki = DES3Util.byte2hex(DES3Util.trides_decrypt(DES3Util.hexToBytes("1F26729F19CAF901A855C0420882CC85"), DES3Util.hexToBytes("AE20FDA591CF2EC0AB4F23399C22378E")));
		System.out.println(ki);
	}

	/**
	 * 对文件进行解密
	 *
	 * @param srcFile
	 *            加密文件
	 * @param desFile
	 *            解密文件
	 *
	 * @param keyStr
	 *            加密后的解密密钥
	 * @throws Exception
	 */
	public static void desDecryptFile(String srcFile, String desFile, String keyStr)
			throws Exception {
		// byte gKeys[] = hexToBytes(gKeyStr);
		byte fKeys[] = hexToBytes(keyStr);

		// byte fileKeys[] = trides_decrypt(gKeys, fKeys); //解密码后文件加密密钥

		File in = new File(srcFile);
		FileInputStream inFile = new FileInputStream(in);
		FileOutputStream fos = new FileOutputStream(desFile);
		byte[] buffer = new byte[1024];
		int i = 0;
		while ((i = inFile.read(buffer)) != -1) {

//			byte temp[] = cardDataFileDecrypt(buffer, keyStr);
			byte temp[] = trides_decrypt(fKeys, buffer);
			fos.write(temp, 0, i);
		}
		fos.close();
		inFile.close();
	}

/*	private static final int DES_KEY_LENTH = 8;
	private static final int DESede_KEY_LENTH = 24;
	private static final int DESed_KEY_LENTH = 16;
	private static final String ENCRYPT_TYPE = "DESede/ECB/NoPadding";

	public static byte[] cardDataFileDecrypt(byte[] srcData, String dataKeys)
			throws Exception {

		// 装载BC加解密方法

		Security.addProvider(new BouncyCastleProvider());

		// 由于提供的key是16进制表示，因此把key转成字节

		byte[] regKey = Hex.decode(dataKeys);

		if (regKey.length == DESed_KEY_LENTH) {

			byte[] temp = regKey;

			regKey = new byte[DESede_KEY_LENTH];

			for (int i = 0; i < temp.length; i++) {

				regKey[i] = temp[i];

			}

			for (int i = 0; i < DES_KEY_LENTH; i++) {

				regKey[DESed_KEY_LENTH + i] = temp[i];

			}

		} else if (regKey.length != DESede_KEY_LENTH) {

			throw new Exception("密钥长度不够" + regKey.length);

		}

		DESedeKeySpec dks = new DESedeKeySpec(regKey);

		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede"

		, BouncyCastleProvider.PROVIDER_NAME);

		SecretKey securekey = keyFactory.generateSecret(dks);

		Cipher cipher = Cipher.getInstance(ENCRYPT_TYPE);

		cipher.init(Cipher.DECRYPT_MODE, securekey);

		return cipher.doFinal(srcData);

	}*/




}
